package Interface;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import Main.*;
import Process.*;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

/**
 *
 * @author Sess
 */
public class MainMenu extends JFrame implements ActionListener {

    Hearts control;
    JTextField jTextFieldPlayerName;
    JPasswordField jPasswordFieldPassword;

    public MainMenu(Hearts control) {
        this.control = control;
        
        try {
            // Set cross-platform Java L&F (also called "Metal")
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        }
        setTitle("Welcome to Our Hearts");
        setResizable(false);
        setSize(300, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        getContentPane().setLayout(new GridBagLayout());

        JLabel jLabelPlayerName = new JLabel("Player Name:");
        AddComponent(0, 0, 1, 1, jLabelPlayerName);

        jTextFieldPlayerName = new JTextField();
        AddComponent(1, 0, 1, 1, jTextFieldPlayerName);

     

        JButton jButtonCreate = new JButton("Create Game");
        jButtonCreate.setActionCommand("Create");

        jButtonCreate.addActionListener(this);
        AddComponent(2, 0, 1, 1, jButtonCreate);

         JButton jButtonJoin = new JButton("Join Game");
        jButtonJoin.setActionCommand("Join");

        jButtonJoin.addActionListener(this);
        AddComponent(3, 0, 1, 1, jButtonJoin);

        JButton jButtonExit = new JButton("Exit");
        jButtonExit.setActionCommand("Exit");

        jButtonExit.addActionListener(this);
        AddComponent(5, 0, 1, 1, jButtonExit);

        jTextFieldPlayerName.setText("Socola.YaphetS");
        
        
    }

    public void actionPerformed(ActionEvent e) {

        String strActionCommand = e.getActionCommand();

        if (strActionCommand.equals("Create")) {
              control.PlayerName = jTextFieldPlayerName.getText();
              System.out.println("Player Name: " + control.PlayerName);
              if (control.PlayerName.equals(""))
                  return;
             
            
            //Main M=new Main();
            //M.go();
            control.ShowServerInterfaceFrame();
            
            
              //control.frameServerInterface.setVisible(true);
              //control.SP = new ServerProcess(control);
        }

        if (strActionCommand.equals("Join")) {
              control.PlayerName = jTextFieldPlayerName.getText();
              System.out.println("Player Name: " + control.PlayerName);
               if (control.PlayerName.equals(""))
                  return;
              control.ShowJoinGameFrame();
              control.frameMainMenu.setVisible(false);
        }
    }

    private void AddComponent(int row, int column, int Gridwidth, int GridHeight, JComponent comp) {
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = column;
        gbc.gridy = row;
        gbc.gridwidth = Gridwidth;
        gbc.gridheight = GridHeight;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets.set(5, 5, 5, 5);
        getContentPane().add(comp, gbc);
    }
}
